﻿using AbpApplication.AppServices;
using AbpApplication.Dto;
using AbpApplication.Expand;
using AbpToDomain.EntityInfo;
using Azure;
using Flurl.Http;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using wms_wpf.Apiservice;

namespace AbpApplication.Flurl
{
    public class ToOutSideInterFace : IToOutSideInterFace
    {

        private readonly ILoginHelper loginHelper;

        public ToOutSideInterFace(ILoginHelper loginHelper)
        {
            this.loginHelper = loginHelper;
        }

        public async Task<bool> moveContainer(string BoxCode, string direction, string slotCode)
        {
            try
            {
                Ess_Input2 input = new Ess_Input2();
                input.direction = direction;
                input.slotCode = slotCode;
                input.containerCode = BoxCode;
                var url = $"http://{GlobalVariable.EssIp}/conveyor/moveContainer";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public async Task<bool> sendCommand(string color, string tagcode, string display)
        {

            try
            {
                Ess_Input4 InPut = new Ess_Input4()
                {
                    color = color,
                    tagCode = tagcode,
                    display = display,
                    mode = "LIGHT"
                };
                var data = InPut;
                var url = $"http://{GlobalVariable.EssIp}/ptl/sendCommand";
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                if (Response.msg == "success")
                {
                    loginHelper.LoginInfoAsync($"sendCommand-亮灯成功+{InPut.tagCode + InPut.display + Response.msg}");
                    return true;
                }
                else
                {
                    loginHelper.LoginInfoAsync($"sendCommand-亮灯失败+{InPut.tagCode + InPut.display + Response.msg}");
                    return false;
                }

            }
            catch
            {
                loginHelper.LoginInfoAsync($"sendCommand-亮灯失败");
                return false;
            }




        }   
        public async Task<string> CreateTask(Ess_Input input)
        {
            try
            {
                var url = $"http://{GlobalVariable.EssIp}/task/create";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    loginHelper.LoginInfoAsync($"msg:{Response.msg},res:{res}");
                    return res;
                }
                else
                {
                    loginHelper.LoginInfoAsync($"msg:{Response.msg},res:{res}");
                    return Response.msg;
                }

            }
            catch (Exception ex)
            {
                loginHelper.LoginInfoAsync($"msg:{ex}");
                return null;
            }
        }
        public async Task<bool> receiveInWarehouseResult(Custom_InPut InPut)
        {
            try
            {
         
                var url = $"http://{GlobalVariable.CustomIp}/api/services/towcs/receiveInWarehouseResult";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseMes Response = JsonConvert.DeserializeObject<ApiResponseMes>(result);    
                if (Response.code == "200")
                {
               
                    return true;
                }
                else
                {
                    loginHelper.LoginInfoAsync($"入库通知失败:{InPut.symbol}+{InPut.boxCode}+{Response.code}+{Response.msg}");
                    return false;
                }

            }
            catch (Exception ex)
            {
                var json= JsonConvert.SerializeObject(InPut);
                loginHelper.LoginInfoAsync($"入库通知错误:{ex.ToString()}+json:{json}");
                return false;
            }
        }
        public async Task<bool> receiveOutWarehouseResult(CustomOut_InPut InPut)
        {
            try
            {
          
                var url = $"https://{GlobalVariable.CustomIp}/api/services/towcs/receiveOutWarehouseResult";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseMes Response = JsonConvert.DeserializeObject<ApiResponseMes>(result);
                if (Response.code == "200")
                {
                
                    return true;
                }
                else
                {
                    loginHelper.LoginInfoAsync($"出库通知失败:{InPut.symbol}+{InPut.boxCode}+{Response.code}+{Response.msg}");
                    return false;
                }

            }
            catch (Exception ex)
            {
                var json = JsonConvert.SerializeObject(InPut);
                loginHelper.LoginInfoAsync($"出库通知错误:{ex.ToString()}+{InPut.symbol}+{InPut.boxCode}+json:{json}");
                return false;
            }
        }
    }
}
